import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import fs from 'fs'
import path from 'path'

// 模拟数据存储路径
const MOCK_DATA_DIR = path.resolve(__dirname, '.mock-data')
if (!fs.existsSync(MOCK_DATA_DIR)) {
    fs.mkdirSync(MOCK_DATA_DIR, { recursive: true })
}
const MOCK_USERS_FILE = path.join(MOCK_DATA_DIR, 'users.json')

// 读取模拟用户数据
function readMockUsers() {
    if (!fs.existsSync(MOCK_USERS_FILE)) {
        return []
    }
    try {
        return JSON.parse(fs.readFileSync(MOCK_USERS_FILE, 'utf-8'))
    } catch (e) {
        console.error('读取模拟用户数据失败:', e)
        return []
    }
}

// 保存模拟用户数据
function saveMockUsers(users) {
    try {
        fs.writeFileSync(MOCK_USERS_FILE, JSON.stringify(users, null, 2))
        return true
    } catch (e) {
        console.error('保存模拟用户数据失败:', e)
        return false
    }
}

export default defineConfig({
    plugins: [
        vue(),
        createSvgIconsPlugin({
            iconDirs: [resolve(process.cwd(), 'src/icons/svg')],
            symbolId: 'icon-[name]'
        })
    ],
    resolve: {
        alias: {
            '@': resolve(__dirname, './src')
        },
        extensions: ['.js', '.json', '.jsx', '.mjs', '.ts', '.tsx', '.vue']
    },
    define: {
        'import.meta.env.VITE_API_BASE_URL': JSON.stringify('http://localhost:8080')
    },
    server: {
        host: '0.0.0.0',
        port: 5173,
        open: true,
        cors: true,
        force: true,
        // https: true,
        proxy: {
            '/api': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ''),
                secure: false,
                configure: (proxy, _options) => {
                    proxy.on('error', (err, _req, _res) => {
                        console.log('代理错误', err);
                    });
                    proxy.on('proxyReq', (proxyReq, req, _res) => {
                        console.log('发送请求到:', req.method, req.url);
                    });
                    proxy.on('proxyRes', (proxyRes, req, _res) => {
                        console.log('收到响应:', proxyRes.statusCode, req.url);
                    });
                }
            },
            '/captcha': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                secure: false,
            },
            '/system/user/register': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                secure: false,
            },
            '/auth/login': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                secure: false,
            },
            '/house/hot': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                secure: false,
            },
            '/house/latest': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                secure: false,
            },
            '/agent/apply': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                secure: false,
            },
            '/api/agent/application/list': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ''),
                secure: false,
            },
            '/api/agent/application/statistics': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ''),
                secure: false,
            },
            '/api/agent/application/approve': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ''),
                secure: false,
            },
            '/api/agent/application/reject': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ''),
                secure: false,
            },
            '/agent/review/list': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                secure: false,
            },
            '/agent/review/statistics': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                secure: false,
            },
            '/agent/review/approve': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                secure: false,
            },
            '/agent/review/reject': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                secure: false,
            },
            '/amap': {
                target: 'https://restapi.amap.com',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/amap/, '')
            }
        }
    },
    build: {
        chunkSizeWarningLimit: 1500,
        rollupOptions: {
            output: {
                manualChunks(id) {
                    if (id.includes('node_modules')) {
                        return 'vendor'
                    }
                }
            }
        }
    }
})